package com.hsd.whb.controller;

import com.hsd.whb.converter.DeviceConverter;
import com.hsd.whb.entity.Device;
import com.hsd.whb.entity.MeetingMaterals;
import com.hsd.whb.enums.ResultEnum;
import com.hsd.whb.pojo.output.DeviceVO;
import com.hsd.whb.pojo.output.ResultVO;
import com.hsd.whb.pojo.query.QueryDevice;
import com.hsd.whb.service.DeviceService;
import com.hsd.whb.service.ManagerService;
import com.hsd.whb.service.MeetingMateralsService;
import com.hsd.whb.utils.ResultVOUtil;
import com.hsd.whb.utils.SecurityUtil;
import com.hsd.whb.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

@Controller
@RequestMapping("/root")
public class RootController {

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private ManagerService managerService;

    @GetMapping("/deviceList")
    public String toDeviceList() {
        return "root/deviceList";
    }

    @PostMapping("/findDeviceList")
    @ResponseBody
    public List<DeviceVO> findDeviceList(@PageableDefault(sort = "accessKey") Pageable pageable, QueryDevice queryDevice) {
        List<Device> list = deviceService.findAllDeviceList(pageable, queryDevice);
        return DeviceConverter.convertToDeviceVOList(list, managerService);
    }

    @GetMapping("/findDeviceList")
    @ResponseBody
    public long findDeviceListTotalPages(Integer size, QueryDevice queryDevice) {
        long totalNum = deviceService.findAllDeviceListTotalPages(queryDevice);
        ;
        return (long) Math.ceil(totalNum / (double) size);
    }

    @GetMapping("/deviceForm")
    public String toDeviceForm() {
        return "root/deviceForm";
    }

    @GetMapping("/findDeviceById")
    @ResponseBody
    public DeviceVO findDeviceById(Integer id) {
        return DeviceConverter.convertToDeviceVO(deviceService.findDeviceById(id), managerService);
    }

    @PostMapping("/deleteDeviceById")
    @ResponseBody
    public ResultVO deleteDeviceById(Integer id) {
        Device device = deviceService.findDeviceById(id);
        if (device.getManagerId() != null) return ResultVOUtil.error(ResultEnum.DEVICE_EXIST_BINDING);
        deviceService.deleteDeviceById(id);
        return ResultVOUtil.success("删除成功");
    }

    @PostMapping("/updateDevice")
    @ResponseBody
    public ResultVO updateDevice(@Valid Device device, BindingResult bindingResult) {
        ValidatorUtil.valid(bindingResult);
        String msg;
        if (device.getId() != null) {
            msg = "修改成功";
        } else {
            msg = "添加成功";
        }
        deviceService.updateDevice(device);
        return ResultVOUtil.success(msg);
    }

}
